
# 安装python环境
# 安装playwright: pip install playwright
# 及浏览器: playwright install

from playwright.sync_api import sync_playwright
import time


def get_info(browser,href):
    page_info = browser.new_page()
    page_info.goto(f'https:{href}')
    page_info.mouse.wheel(0, 100000)
    time.sleep(3)
    percent = page_info.query_selector_all('xpath=//*[@id="comment"]/div[2]/div[1]/div[1]/div')[0].inner_text()
    page_info.close()
    return {'percent':percent}



def get_list(browser,search_str):
    page = browser.new_page()
    page.goto(f'https://search.jd.com/Search?keyword={search_str}')
    li_list = page.query_selector_all('xpath=//*[@id="J_goodsList"]/ul/li')
    
    def parse_li(li):
        title = li.query_selector_all('xpath=//em')[1].inner_text()
        price = li.query_selector_all('xpath=//i')[0].inner_text()
        href = li.query_selector_all('xpath=//a')[0].get_attribute('href')
        
        obj={
            'title':title,
            'price':price,
            'href':href,
        } 
        return obj 
    li_obj_list=list(map(parse_li,li_list))
    
    page.close()
    return li_obj_list
    

def main(search_str):
    with sync_playwright() as p:
        browser_type = p.chromium
        # browser = browser_type.launch(headless=False)
        browser = browser_type.launch(headless=True)
        lis = get_list(browser,search_str)
        # lis = lis[:2]
        
        lis = [ {**li,**get_info(browser,li['href'])} for li in lis]
        
        print(lis)
        pass
    
if __name__ == '__main__':
    main('电脑')